package loan.calc.service.loan.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import loan.calc.domain.LoanRepaySegmentDO;
import loan.calc.dto.LoanRepaySegmentDTO;
import loan.calc.mapper.LoanRepaySegmentMapper;
import loan.calc.service.loan.LoanRepaySegmentService;
import loan.calc.util.POJOConverter;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LoanRepaySegmentServiceImpl extends ServiceImpl<LoanRepaySegmentMapper, LoanRepaySegmentDO> implements LoanRepaySegmentService {

    @Override
    public void setSegmentId(LoanRepaySegmentDTO loanRepaySegment, Long id) {
        loanRepaySegment.setSegmentId(id);
        if (loanRepaySegment.getLoanRepaymentScheduleList() != null && loanRepaySegment.getLoanRepaymentScheduleList().size() > 0) {
            loanRepaySegment.getLoanRepaymentScheduleList().forEach(i -> i.setRepaySegmentId(id));
        }
    }

    @Override
    public List<LoanRepaySegmentDTO> getLoanRepaySegmentListByLoanId(Long loanId) {
        return lambdaQuery().eq(LoanRepaySegmentDO::getLoanId, loanId).list().stream().map(domain -> POJOConverter.convert(LoanRepaySegmentDTO.class, domain)).collect(Collectors.toList());
    }
}
